package app.android.kdictionary.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import app.android.kdictionary.R;
import app.android.kdictionary.R.id;
import app.android.kdictionary.Widget.ArrayWheelAdapter;
import app.android.kdictionary.Widget.WheelView;

public class PickerActivity extends Activity {
	private ImageButton setButton;
	
	private WheelView picker1;
	private WheelView picker1_o;
	private WheelView picker2;
	
	private String[] strPicker1;
	private String[] strPicker1_o;
	private String[] strPicker2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picker);
        
        findViewById();
        initTask();
    }
    
    private void findViewById() {
    	picker1 = (WheelView) findViewById(R.id.picker1);
    	picker1_o = (WheelView) findViewById(R.id.picker1_o);
        picker2 = (WheelView) findViewById(R.id.picker2);
        setButton = (ImageButton)findViewById(id.main_picker_button);
    }
    
    private void initTask() {
        strPicker1 = getResources().getStringArray(R.array.picker_list_1);
        strPicker2 = getResources().getStringArray(R.array.picker_list_2);
        
        picker1.setVisibleItems(5);
        picker1.setAdapter(new ArrayWheelAdapter<String>(strPicker1));
        picker1.setCurrentItem(0);
        
        picker1.addChangingListener(new app.android.kdictionary.Widget.OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if( newValue==1 ) {        // 원어
					strPicker1_o = getResources().getStringArray(R.array.picker_option_list_1);
					picker1_o.setVisibleItems(5);
					picker1_o.setAdapter(new ArrayWheelAdapter<String>(strPicker1_o));
					picker1_o.setCurrentItem(0);
					picker1_o.setVisibility(View.VISIBLE);
				}
				else if( newValue==13 ) {        // 전문어
					strPicker1_o = getResources().getStringArray(R.array.picker_option_list_2);
					picker1_o.setVisibleItems(5);
					picker1_o.setAdapter(new ArrayWheelAdapter<String>(strPicker1_o));
					picker1_o.setCurrentItem(0);
					picker1_o.setVisibility(View.VISIBLE);
				}
				else {			// 기타
					picker1_o.setVisibility(View.GONE);
				}
		    }
		});        
        
        picker2.setVisibleItems(5);
        picker2.setAdapter(new ArrayWheelAdapter<String>(strPicker2));
        picker2.setCurrentItem(0);
        
        setButton.setOnClickListener(mSetListener);
    }
    
    private OnClickListener mSetListener = new OnClickListener() {
    	public void onClick(View v) {
			EditText editText = (EditText)findViewById(id.main_picker_edit);
			
			Intent result = new Intent(getBaseContext(), DetailActivity.class);
			result.putExtra("picker1", picker1.getCurrentItem());
			int currentItem = picker1.getCurrentItem();
			if( currentItem==1 ) {        // 원어
				result.putExtra("picker1_o", picker1_o.getCurrentItem());
			}
			else if( currentItem==13 ) {        // 전문어
				result.putExtra("picker1_o", picker1_o.getCurrentItem());
			}
			else {
				result.putExtra("picker1_o", -1);
			}
			result.putExtra("picker2", picker2.getCurrentItem());
			result.putExtra("text", editText.getText().toString());
			
			setResult(RESULT_OK, result);
			
			finish();
    	}
    };

}

